#ifndef _HEADER_WaterSys_H
#include "WaterSys.h"
#endif

void WaterSys::init(WaterSysConfig* inConfig)
{
    //初始化自来水罐
    water0.outPump.setPort(inConfig->waterTank0_outPump_port);
    water0.waterLine.setPort(inConfig->waterTank0_waterLine_port);
    water0.waterSwitchIn.setPort(inConfig->waterTank0_waterSwitchIn_port);
    water0.waterSwitchOut.setPort(inConfig->waterTank0_waterSwitchOut_port);

    //初始化雨水罐
    water1.outPump.setPort(inConfig->waterTank1_outPump_port);
    water1.waterLine.setPort(inConfig->waterTank1_waterLine_port);
}

/**自来水罐的自动任务
 * 用自来水或者雨水罐把自己灌满，尽量用雨水
 * */
void WaterSys::autoWork()
{
    if(water0.isFull())
    {
        //如果自来水罐已满，关闭自来水开关和雨水罐泵
        water0.closeWaterIn();
        water1.closeOutPump();
    }
    else
    {
        //否则判断雨水罐是否为空
        if(water1.isEmpty())
        {
            //为空打开自来水入水开关
            water0.openWaterIn();
            water1.closeOutPump();
        }
        else
        {
            //否则打开雨水罐的泵
            water1.openOutPump();
            water0.closeWaterIn();
        }
    }
}

/**
 * 供水系统关闭
 * */
void WaterSys::close()
{
    water0.outPump.close();
    water0.closeWaterOut();
}

/**
 * 供水系统是否就绪判断：自来水罐为满时为就绪，其他状态都为非就绪
 * */
bool WaterSys::isReady()
{
    isReadyState = water0.isFullState;
    return isReadyState;
}

/**
 * 供水系统打开
 * */
void WaterSys::open()
{
    water0.outPump.open();
    water0.openWaterOut();
}

void WaterSys::toString(char* inStr)
{
    char tmp[200],tmp1[200];
    this->water0.toString(tmp);
    this->water1.toString(tmp1);
    sprintf(inStr,"WaterSys:{isReadyState=%d,%s,%s}",isReadyState,tmp,tmp1);
}
